<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class GoodsController extends Controller
{   
    //商品添加页面
	public function getAdd()
    {   
        $cates=SortController::getSorts();
        
        return view('admin.goods.add',['cates'=>$cates]);
    }

    //商品添加
    public function postInsert(Request $request)
    {   
        $data=$request->except(['_token']);
        //调用图片上传方法
        $re=self::Upload($request,'goodpic');
        $data['goodpic']=$re['goodpic'];
            

        $res=DB::table('goods')->insert($data);
            if($res){
                return redirect('admin/goods/index')->with('success','添加成功');
            }else{
                return back()->with('error','添加失败');
            }
    }

    // 商品列表
    public function getIndex(Request $request)
    {	
        //分页
        $goods=DB::table('goods')
        ->orderBy('id','desc')
        ->where(function($query)use($request){
            $query->where('goodname','like','%'.$request->input('keywords').'%%');
        })
        ->paginate($request->input('num',10));
        // dd($users);
        return view('admin.goods.index',['goods'=>$goods,'request'=>$request->all()]);
    }	

    //修改商品页面
    public function getEdit($id,$sortid)
    {
        // dd($sortid);
        $res=DB::table('goods')->where('id',$id)->first();
        $re=DB::table('sort')->where('sorid',$sortid)->get();


        return view('admin.goods.edit',['goods'=>$res,'sort'=>$re]);

    }
        /**
         * 修改商品
         */
    public function postUpdate(Request $request)
    {
        $data=$request->except(['_token','id']);
        
        //调用图片上传方法
        $res=self::Upload($request,'goodpic');

        $data['goodpic']=$res['goodpic'];
        
        $res=DB::table('goods')->where('id',$request->id)->update($data);
            if($res){
                return redirect('admin/goods/index')->with('success','修改成功');
            }else{
                return back()->with('error','修改失败');
            }
    }

    /**
     * 删除商品
     */
    public function getDelete($id)
    {
        $res=DB::table('goods')->where('id',$id)->delete();
        
        if($res){
            return redirect('/admin/goods')->with('success','商品删除成功');
        }else{
            return back()->with('error','商品删除失败');
        }
    }
    



    //图片上传
    public static function Upload(Request $request,$file)
    {
        if($request->hasFile($file)){
            $hzm = $request->file($file)->getClientOriginalExtension();
            $name = md5(time().rand(0,19999999));
            $request->file($file)->move(Config::get('app.upload_dir'), $name.'.'.$hzm);
            $data[$file]=trim(Config::get('app.upload_dir').$name.'.'.$hzm,'.');
            return $data;
        }
    }



	/**
	 * 列表页,根据商品分类id查询商品表
	 */
	public function getList($sorid)
	{
		//根据sorid到商品goods表中查询数据 状态为0
		$sortid = $sorid;
		$goods = DB::table('goods')
			->where('sortid',$sorid)
			->where('status',0)
			->get();
        $lianjie = IndexController::listss();
		//显示模板 分配变量
    	return view('homegoods.list',['goods'=>$goods,'sortid'=>$sortid,'lianjie'=>$lianjie]);
	}

	/**
	 * 列表页,根据商品分类id查询商品表
	 */
	public function getTwo($sortid)
	{
		//根据sorid到商品goods表中查询数据  状态为1
		$goods = DB::table('goods')
			->where('sortid',$sortid)
			->where('status',1)
			->get();
        $lianjie = IndexController::listss();
		//显示模板 分配变量
    	return view('homegoods.list',['goods'=>$goods,'sortid'=>$sortid,'lianjie'=>$lianjie]);
	}

	/**
	 * 列表页,根据商品分类id查询商品表
	 */
	public function getThree($sortid)
	{
		//根据sorid到商品goods表中查询数据  状态为2
		$goods = DB::table('goods')
			->where('sortid',$sortid)
			->where('status',2)
			->get();
        $lianjie = IndexController::listss();
		//显示模板 分配变量
    	return view('homegoods.list',['goods'=>$goods,'sortid'=>$sortid,'lianjie'=>$lianjie]);
	}
}
